package com.lingrit.provider.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

public class LogUilts {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void addLog(LinkedList<String> logList, String startDate, String endDate, String table){
        Connection conn = EtlDbUtils.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareCall("insert into data_sync_log(id,task_start,task_end,task_time,task_flow,write_speed,read_number,fail_number,del_flag,is_success,tb_name) values(?,?,?,?,?,?,?,?,?,?,?)");

            ps.setString(1, UUID.randomUUID().toString());
            ps.setString(9, "0");
            ps.setString(10, Constant.SUCCESS);
            ps.setString(11, table);
            String[] logSplit = new String[7];
            List<String> loglist =  logList.subList((logList.size()-7), logList.size());
            loglist.toArray(logSplit);

            if(!(logSplit[0].split(":")[0].trim()).equals(Constant.TASK_START_STR)){
                //错误日志
                ps = conn.prepareCall("insert into data_sync_log(id,task_start,task_end,task_time,del_flag,is_success,tb_name) values(?,?,?,?,?,?,?)");
                ps.setString(1, UUID.randomUUID().toString());
                ps.setTimestamp(2,new java.sql.Timestamp(dateFormat.parse(startDate).getTime()));
                ps.setTimestamp(3,new java.sql.Timestamp(dateFormat.parse(endDate).getTime()));
                ps.setString(4, String.valueOf((dateFormat.parse(endDate).getTime() -
                        dateFormat.parse(startDate).getTime())/1000) + "s");
                ps.setString(5, "0");
                ps.setString(6, Constant.FAIL);
                ps.setString(7, table);
                ps.executeUpdate();
                return;
            }

            for (int i = 0; i < logSplit.length; i++) {
                switch (i){
                    case 0:
                        String taskStart = (logSplit[i].split(" : ")[1]).trim();
                        ps.setTimestamp(2,new java.sql.Timestamp(dateFormat.parse(taskStart).getTime()));
                        break;
                    case 1:
                        String taskEnd = (logSplit[i].split(" : ")[1]).trim();
                        ps.setTimestamp(3,new java.sql.Timestamp(dateFormat.parse(taskEnd).getTime()));
                        break;
                    case 2:
                        String taskTime= (logSplit[i].split(" : ")[1]).trim();
                        ps.setString(4,taskTime);
                        break;
                    case 3:
                        String taskFlow = (logSplit[i].split(" : ")[1]).trim();
                        ps.setString(5,taskFlow);
                        break;
                    case 4:
                        String readNumber = (logSplit[i].split(" : ")[1]).trim();
                        ps.setString(6,readNumber);
                        break;
                    case 5:
                        String writeSpeed = (logSplit[i].split(" : ")[1]).trim();
                        ps.setString(7,writeSpeed);
                        break;
                    case 6:
                        String failNumber = (logSplit[i].split(" : ")[1]).trim();
                        ps.setString(8,failNumber);
                        break;
                    default:
                        break;
                }
            }
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
